package com.lin.operatorlogrecord.unit;

import com.lin.operatorlogrecord.compoment.SpringBeanContext;
import com.lin.operatorlogrecord.entity.constant.GlobalConstant;
import com.lin.operatorlogrecord.entity.enums.ObjectTypeEnum;
import com.lin.operatorlogrecord.exception.OperatorLogException;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chenghao.lin
 * @Date: 2023/09/25
 * @Description:
 */
public class OperatorLogQueryUtil {

    private OperatorLogQueryUtil(){}

    private static JdbcTemplate jdbcTemplate;

    /**
     * 获取Jdbc静态类
     */
    private static JdbcTemplate getJdbcTemplate() {
        if (null == jdbcTemplate) {
            jdbcTemplate = SpringBeanContext.getSpringBean(JdbcTemplate.class);
        }
        return jdbcTemplate;
    }


    /**
     * 通过Id查询获取数据
     * @param operatorObjIds
     * @param tableEntity
     * @return
     */
    public static List<Map<String, Object>> listObj(List<String> operatorObjIds, Class<?> tableEntity) {
        if (CollectionUtils.isEmpty(operatorObjIds)) {
            throw new OperatorLogException("查询旧数据，主键列表Id不能为空");
        }
        // 获取表名
        String tableName = OperatorLogUtil.getTableName(tableEntity);

        String querySql =  String.format("select * from %s where %s in (%s);", tableName, GlobalConstant.TABLE_ID_COLUMN_NAME, String.join("," , operatorObjIds));
        return getJdbcTemplate().queryForList(querySql);
    }


    /**
     * 通过Id查询获取数据
     * @param operatorObjId
     * @param tableEntity
     * @return
     */
    public static Map<String, Object> getObj(String operatorObjId, Class<?> tableEntity) {
        if (StringUtils.isBlank(operatorObjId)) {
            throw new OperatorLogException("查询数据，主键列表Id不能为空");
        }
        // 获取表名
        String tableName = OperatorLogUtil.getTableName(tableEntity);
        String querySql = String.format("select * from %s where %s = %s;", tableName, GlobalConstant.TABLE_ID_COLUMN_NAME, operatorObjId);
        return getJdbcTemplate().queryForMap(querySql);
    }

    public static Map<String, String> listObjIdAndNameMap(List<String> objIds, ObjectTypeEnum objectTypeEnum) {
        Map<String, String> result = new HashMap<>();
        if (CollectionUtils.isEmpty(objIds)) {
            return  result;
        }
        String sql = String.format("select %s, %s from %s where %s in (%s);", GlobalConstant.TABLE_ID_COLUMN_NAME, GlobalConstant.TABLE_NAME_COLUMN_NAME,
                objectTypeEnum.getTableName(), GlobalConstant.TABLE_ID_COLUMN_NAME, String.join(",", objIds));
        try {
            List<Map<String, Object>> queryList = getJdbcTemplate().queryForList(sql);
            for (Map<String, Object> queryMap : queryList) {
                result.put(queryMap.get(GlobalConstant.TABLE_ID_COLUMN_NAME).toString(), queryMap.get(GlobalConstant.TABLE_NAME_COLUMN_NAME).toString());
            }
        } catch (Exception e) {
            return result;
        }
        return result;
    }


}
